//
//  BankCardViewController.m
//  阿帕
//
//  Created 阿帕 on 2019/12/24.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "BankCardViewController.h"
#import "GDBandCardAddViewController.h"
#import "BindCardCell.h"
#import "GlobalPopUpTool.h"
#import "GeneralPopUp.h"
#import "NYSExcDescView.h"
#import "FFPopup.h"
#import "ZTXSelecteBankView.h"
#import "ZTXBangDingBackCodeView.h"
#import "BandCardAddViewController.h"

@interface BankCardViewController ()<UITableViewDelegate,UITableViewDataSource>{
    BankCardListModel *cussentModel;
}

@property (strong , nonatomic)UITableView *tableView;

@property (strong , nonatomic)NSMutableArray *dataArray;

@property (strong , nonatomic)UIImageView *foundImageView;

@property (strong , nonatomic)UIButton *addCardButton;

@property (nonatomic, assign) NSInteger pageNum;

@property (strong, nonatomic) NYSExcDescView *exDescView;
@property (nonatomic, strong) ZTXSelecteBankView *bangDingBarkView;
@property (nonatomic, strong) NSString *jianQuanCode;
@property (nonatomic, strong)ZTXBangDingBackCodeView *backCodeview;
@end
static NSString *const cellID = @"BankCardCellID";

static const CGFloat cellHeight = 117;

@implementation BankCardViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self bankCardListRequest];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _pageNum = 1;
    
    self.view.backgroundColor = BackgroundColor;
    
    [self setupBaseView];
}

- (void)setupBaseView {
    
    self.navigationItem.title = @"我的银行卡";
    
    UIView *topBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, SX(30))];
    topBgView.backgroundColor = [UIColor colorWithHexString:@"#DEE6F2"];
    [self.view addSubview:topBgView];
    
    UIImageView *lImg = [[UIImageView alloc] initWithFrame:CGRectMake(SX(15), 0, SX(14.5f), SY(13.5f))];
    lImg.image = image(@"gcy_my_taxauthmsg");
    lImg.centerY = topBgView.centerY;
    [topBgView addSubview:lImg];
    
    UILabel *tLabel = [[UILabel alloc] init];
    tLabel.textColor = Theme_ColorValue;
    tLabel.text = @"新增银行卡后需要在该张默认银行卡后面点绑卡操作";
    tLabel.font = font(13);
    tLabel.numberOfLines = 1;
    tLabel.adjustsFontSizeToFitWidth = YES;
    [topBgView addSubview:tLabel];
    [tLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(SX(35));
        make.right.mas_equalTo(-SX(5));
        make.height.equalTo(topBgView);
    }];
    
    [self.view addSubview:self.tableView];
    
    [self.view addSubview:self.addCardButton];
    
}

- (void)bankCardListRequest {
    WEAKSELF
    if (_pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *params = @{
        @"pageNum": [NSNumber numberWithInteger:_pageNum],
        @"pageSize": DEFULT_LOAD_NUM,
        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
    };
    
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], BANKCARD) withParameter:params withVC:self netName:@"银行卡列表" success:^(id response, NSString *msg) {
        
        if ([response[@"records"] count] != 0) {
            
            [weakSelf.dataArray addObjectsFromArray:[BankCardListModel mj_objectArrayWithKeyValuesArray:response[@"records"]]];
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            
            weakSelf.foundImageView.hidden = YES;
            weakSelf.tableView.hidden = NO;
            
        } else {
            if (_pageNum == 1) {
                weakSelf.foundImageView.hidden = NO;
                weakSelf.tableView.hidden = YES;
            }
            
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            
        }
        
        // 首张银行卡自动默认
        if (weakSelf.dataArray.count == 1) {
            BankCardListModel *model = [weakSelf.dataArray firstObject];
            if (!model.isDefault) {
                [HTTPManager putNetworkRequest:CJStringWithFormat(@"%@%@/%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],BANKCARD_SETDEFAULTCARD,model.code) withParameter:nil withVC:self netName:@"设置默认银行卡" success:^(id response, NSString *msg) {
                    [weakSelf loadNewData];
                } failure:^(NSInteger code, NSString *msg) {

                }];
            }
        }
        
        [weakSelf.tableView reloadData];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

//上拉刷新数据
- (void)loadNewData{
    _pageNum = 1;
    [self bankCardListRequest];
}
- (void)loadMoreData{
    _pageNum ++;
    [self bankCardListRequest];
}

- (void)btnEventClick {
    
    GDBandCardAddViewController *bcavc = [[GDBandCardAddViewController alloc] init];
    NSString *paymentType = [[UserDefaultUtil valueForKey:Login_Branch] objectForKey:@"paymentType"];
    bcavc.isSelect = [paymentType isEqualToString:@"mybank"] ? NO : YES;
    [self.navigationController pushViewController:bcavc animated:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BindCardCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = self.dataArray[indexPath.row];
    cell.buttonTitleBlock = ^(NSString * _Nonnull titleStr) {
        WEAKSELF
        BankCardListModel *model = self.dataArray[indexPath.row];
        cussentModel = model;
        if([titleStr isEqualToString:@"默认"]){
            [HTTPManager putNetworkRequest:CJStringWithFormat(@"%@%@/%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],BANKCARD_SETDEFAULTCARD,model.code) withParameter:nil withVC:self netName:@"设置默认银行卡" success:^(id response, NSString *msg) {

                [XSInfoView showInfo:msg onView:weakSelf.view];

                [weakSelf bankCardListRequest];

            } failure:^(NSInteger code, NSString *msg) {

            }];
        }else if ([titleStr isEqualToString:@"编辑"]){
            BandCardAddViewController *bcavc = [[BandCardAddViewController alloc] init];
            NSString *paymentType = [[UserDefaultUtil valueForKey:Login_Branch] objectForKey:@"paymentType"];
            bcavc.isSelect = [paymentType isEqualToString:@"mybank"] ? NO : YES;
            bcavc.isEdit = YES;
            bcavc.model = model;
            [self.navigationController pushViewController:bcavc animated:YES];
        }else if ([titleStr isEqualToString:@"删除"]){
            GeneralPopUp *generalPopUp = [[GeneralPopUp alloc] initWithFrame:CGRectMake(0, 0, ScreenW-100, 240)];
            cut(generalPopUp, 5);
            generalPopUp.backgroundColor = [UIColor whiteColor];
            generalPopUp.mainTitle = @"操作提示";
            generalPopUp.subTitle = @"你确定要删除该银行卡吗？";
            generalPopUp.handleBtnTitleArray = @[@"取消", @"确定"];
            [[GlobalPopUpTool sharedInstance] popUpWithView:generalPopUp withViewController:self withAnimation:YES withCanTapBgCancel:YES];
            generalPopUp.btnActionResultBlock = ^(BOOL isConfirm, NSString * _Nonnull title, NSInteger index) {

                [[GlobalPopUpTool sharedInstance] removePopUpView];

                if (isConfirm) {

                    NSDictionary *params = @{
                        @"codes": model.code,
                        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
                    };
                    [HTTPManager deleteNetworkRequest:CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],BANKCARD) withParameter:params withVC:self netName:@"删除银行卡" success:^(id response, NSString *msg) {

                        [XSInfoView showInfo:msg onView:weakSelf.view];

                        // 首先修改model
                        [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
                        // 之后更新view
                        [weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

                    } failure:^(NSInteger code, NSString *msg) {

                    }];
                } else {
                    [weakSelf.tableView reloadData];
                }
            };
        }else if ([titleStr isEqualToString:@"绑卡"]){
            [[UIApplication sharedApplication].keyWindow addSubview:weakSelf.exDescView];
            [self.tableView setEditing:NO animated:YES];
            weakSelf.exDescView.code = model.code;
            weakSelf.exDescView.bindCardStatus = model.bindCardStatus;
            weakSelf.exDescView.hatBindCardStatus = model.hatBindCardStatus;
            weakSelf.exDescView.gdBindCardStatus = model.gdBindCardStatus;
            weakSelf.exDescView.returnBlock = ^(NSString *typeStr) {
                [weakSelf.exDescView removeFromSuperview];
                weakSelf.exDescView = nil;
                [XSInfoView showInfo:typeStr onView:self.view];
            };
            weakSelf.exDescView.block = ^(NSString *code, NSString *merchantCode, NSString *accountType) {
                if (![merchantCode isNotBlank]) {
                    [weakSelf.exDescView removeFromSuperview];
                    weakSelf.exDescView = nil;
                }


                if ([merchantCode isNotBlank]) {
                    if ([accountType intValue] ==0) {
                        [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],BIND_BANK_CARD) withParameter:@{@"merchantCode":merchantCode, @"code":code} withVC:weakSelf netName:@"绑卡" success:^(id response, NSString *msg) {

                            if ([response[@"code"] intValue] == 501) {
                                UIAlertController *alertView=[UIAlertController alertControllerWithTitle:@"绑卡失败,请先完善信息" message: response[@"msg"] preferredStyle:UIAlertControllerStyleAlert];
                                UIAlertAction *sureAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                    BandCardAddViewController *bcavc = [[BandCardAddViewController alloc] init];
                                    NSString *paymentType = [[UserDefaultUtil valueForKey:Login_Branch] objectForKey:@"paymentType"];
                                    bcavc.isSelect = [paymentType isEqualToString:@"mybank"] ? NO : YES;
                                    bcavc.isEdit = YES;
                                    bcavc.model = cussentModel;
                                    [self.navigationController pushViewController:bcavc animated:YES];
                                    return;
                                }];
                                UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"稍后完善" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                                    return;
                                }];
                                [alertView addAction:sureAction];
                                [alertView addAction:cancelAction];
                                [self presentViewController:alertView animated:YES completion:nil];
                            }else{
                                [XSInfoView showInfo:msg onView:weakSelf.view];
                                [weakSelf bankCardListRequest];
                            }
                           
                            [weakSelf.exDescView removeFromSuperview];
                            weakSelf.exDescView = nil;
                        } failure:^(NSInteger code, NSString *msg) {

                        }];
                    }else  if ([accountType intValue] ==1){
                        [weakSelf kuaiqianBangDingNetworking:model.code MerchantCode:merchantCode Phone:model.userPhone];
                    }else  if ([accountType intValue] ==2){
                        [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],GDMERCHANT_BANKCARDFORMULTIPLETAX) withParameter:@{@"merchantCode":merchantCode, @"code":code} withVC:weakSelf netName:@"绑卡光大" success:^(id response, NSString *msg) {

                            [XSInfoView showInfo:msg onView:weakSelf.view];
                            [weakSelf bankCardListRequest];

                            [weakSelf.exDescView removeFromSuperview];
                            weakSelf.exDescView = nil;
                        } failure:^(NSInteger code, NSString *msg) {

                        }];
                    }
                    [self bankCardListRequest];
                }
            };
            [self.bangDingBarkView.cententButton addTarget:self action:@selector(cententBangDingBark) forControlEvents:(UIControlEventTouchUpInside)];
            [self.view addSubview:self.bangDingBarkView];

        }else if ([titleStr isEqualToString:@"解绑"]){
            [[UIApplication sharedApplication].keyWindow addSubview:weakSelf.exDescView];
            [self.tableView setEditing:NO animated:YES];
            weakSelf.exDescView.code = model.code;
            weakSelf.exDescView.bindCardStatus = model.bindCardStatus;
            weakSelf.exDescView.hatBindCardStatus = model.hatBindCardStatus;
            weakSelf.exDescView.gdBindCardStatus = model.gdBindCardStatus;
            weakSelf.exDescView.block = ^(NSString *code, NSString *merchantCode, NSString *accountType) {
                if (![merchantCode isNotBlank]) {
                    [weakSelf.exDescView removeFromSuperview];
                    weakSelf.exDescView = nil;
                }

                
                if ([merchantCode isNotBlank]) {
                    if ([accountType intValue] ==0) {
                        [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@/%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],UNBIND_BANK_CARD,model.code) withParameter:nil withVC:self netName:@"解绑浦发" success:^(id response, NSString *msg) {
                            
                            [XSInfoView showInfo:msg onView:weakSelf.view];
                            [weakSelf.exDescView removeFromSuperview];
                            weakSelf.exDescView = nil;
                            [weakSelf bankCardListRequest];
                        
                        } failure:^(NSInteger code, NSString *msg) {
                            
                        }];
                    }else if ([accountType intValue] ==1) {
                        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
                        [dic setValue:merchantCode forKey:@"merchantCode"];
                        [dic setValue:model.code forKey:@"code"];
                        [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],HATMErCHANT_BANK_CARD) withParameter:dic withVC:self netName:@"解绑快钱" success:^(id response, NSString *msg) {
                            
                            [XSInfoView showInfo:msg onView:weakSelf.view];
                            [weakSelf.exDescView removeFromSuperview];
                            weakSelf.exDescView = nil;
                            [weakSelf bankCardListRequest];
                                                
                        } failure:^(NSInteger code, NSString *msg) {
                            
                        }];
                    }else if ([accountType intValue] ==2) {
                        [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@/%@/%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],GDTMErCHANT_UNBINGBANKCARDFORMULTIPLETAZ,model.code,merchantCode) withParameter:nil withVC:self netName:@"解绑光大" success:^(id response, NSString *msg) {
                            
                            [XSInfoView showInfo:msg onView:weakSelf.view];
                            [weakSelf.exDescView removeFromSuperview];
                            weakSelf.exDescView = nil;
                            [weakSelf bankCardListRequest];
                                                    
                        } failure:^(NSInteger code, NSString *msg) {
                            
                        }];
                    }
                    [self bankCardListRequest];
                }
            };
        }
    };
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
//- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath  API_AVAILABLE(ios(11.0)) {
//    
//    WEAKSELF
//    BankCardListModel *model = self.dataArray[indexPath.row];
//    cussentModel = model;
//    
//    if (@available(iOS 11.0, *)) {
//        
//        // 默认
//        UIContextualAction *setDefultAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"默认" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//            
//            [self.tableView setEditing:NO animated:YES];
//
//            [HTTPManager putNetworkRequest:CJStringWithFormat(@"%@%@/%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],BANKCARD_SETDEFAULTCARD,model.code) withParameter:nil withVC:self netName:@"设置默认银行卡" success:^(id response, NSString *msg) {
//
//                [XSInfoView showInfo:msg onView:weakSelf.view];
//
//                [weakSelf bankCardListRequest];
//
//                completionHandler (YES);
//
//            } failure:^(NSInteger code, NSString *msg) {
//
//            }];
//        }];
//        setDefultAction.backgroundColor = [UIColor colorWithHexString:@"#349DE5"];
//
//        // 编辑
//        UIContextualAction *setEditAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"编辑" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//            
//            [self.tableView setEditing:NO animated:YES];
//
//            BandCardAddViewController *bcavc = [[BandCardAddViewController alloc] init];
//            NSString *paymentType = [[UserDefaultUtil valueForKey:Login_Branch] objectForKey:@"paymentType"];
//            bcavc.isSelect = [paymentType isEqualToString:@"mybank"] ? NO : YES;
//            bcavc.isEdit = YES;
//            bcavc.model = model;
//            [self.navigationController pushViewController:bcavc animated:YES];
//            
//            completionHandler (YES);
//        }];
//        setEditAction.backgroundColor = [UIColor colorWithHexString:@"#F5B51B"];
//        
//        // 删除
//        UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//            
//            deleteAction.backgroundColor = [UIColor redColor];
//
//            // 这句很重要，退出编辑模式，隐藏左滑菜单
//            [self.tableView setEditing:NO animated:YES];
//            
//            GeneralPopUp *generalPopUp = [[GeneralPopUp alloc] initWithFrame:CGRectMake(0, 0, ScreenW-100, 240)];
//            cut(generalPopUp, 5);
//            generalPopUp.backgroundColor = [UIColor whiteColor];
//            generalPopUp.mainTitle = @"操作提示";
//            generalPopUp.subTitle = @"你确定要删除该银行卡吗？";
//            generalPopUp.handleBtnTitleArray = @[@"取消", @"确定"];
//            [[GlobalPopUpTool sharedInstance] popUpWithView:generalPopUp withViewController:self withAnimation:YES withCanTapBgCancel:YES];
//            generalPopUp.btnActionResultBlock = ^(BOOL isConfirm, NSString * _Nonnull title, NSInteger index) {
//
//                [[GlobalPopUpTool sharedInstance] removePopUpView];
//
//                if (isConfirm) {
//
//                    NSDictionary *params = @{
//                        @"codes": model.code,
//                        @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
//                    };
//                    [HTTPManager deleteNetworkRequest:CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],BANKCARD) withParameter:params withVC:self netName:@"删除银行卡" success:^(id response, NSString *msg) {
//
//                        [XSInfoView showInfo:msg onView:weakSelf.view];
//
//                        // 首先修改model
//                        [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
//                        // 之后更新view
//                        [weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//                        
//                        /* 中间为代码删除的具体逻辑实现，进行删除列表接口调用 */
//                        completionHandler(true);
//
//                    } failure:^(NSInteger code, NSString *msg) {
//
//                    }];
//                } else {
//                    [weakSelf.tableView reloadData];
//                }
//            };
//            
//        }];
//        deleteAction.backgroundColor = [UIColor colorWithHexString:@"E05147"];
//        
//        // 绑卡
//        UIContextualAction *bindCardAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"绑卡" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//            [[UIApplication sharedApplication].keyWindow addSubview:weakSelf.exDescView];
//            [self.tableView setEditing:NO animated:YES];
//            weakSelf.exDescView.code = model.code;
//            weakSelf.exDescView.bindCardStatus = model.bindCardStatus;
//            weakSelf.exDescView.hatBindCardStatus = model.hatBindCardStatus;
//            weakSelf.exDescView.gdBindCardStatus = model.gdBindCardStatus;
//            weakSelf.exDescView.returnBlock = ^(NSString *typeStr) {
//                [weakSelf.exDescView removeFromSuperview];
//                weakSelf.exDescView = nil;
//                [XSInfoView showInfo:typeStr onView:self.view];
//            };
//            weakSelf.exDescView.block = ^(NSString *code, NSString *merchantCode, NSString *accountType) {
//                if (![merchantCode isNotBlank]) {
//                    [weakSelf.exDescView removeFromSuperview];
//                    weakSelf.exDescView = nil;
//                }
//
//
//                if ([merchantCode isNotBlank]) {
//                    if ([accountType intValue] ==0) {
//                        [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],BIND_BANK_CARD) withParameter:@{@"merchantCode":merchantCode, @"code":code} withVC:weakSelf netName:@"绑卡" success:^(id response, NSString *msg) {
//
//                            if ([response[@"code"] intValue] == 501) {
//                                UIAlertController *alertView=[UIAlertController alertControllerWithTitle:@"绑卡失败,请先完善信息" message: response[@"msg"] preferredStyle:UIAlertControllerStyleAlert];
//                                UIAlertAction *sureAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                                    BandCardAddViewController *bcavc = [[BandCardAddViewController alloc] init];
//                                    NSString *paymentType = [[UserDefaultUtil valueForKey:Login_Branch] objectForKey:@"paymentType"];
//                                    bcavc.isSelect = [paymentType isEqualToString:@"mybank"] ? NO : YES;
//                                    bcavc.isEdit = YES;
//                                    bcavc.model = cussentModel;
//                                    [self.navigationController pushViewController:bcavc animated:YES];
//                                    return;
//                                }];
//                                UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"稍后完善" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//                                    return;
//                                }];
//                                [alertView addAction:sureAction];
//                                [alertView addAction:cancelAction];
//                                [self presentViewController:alertView animated:YES completion:nil];
//                            }else{
//                                [XSInfoView showInfo:msg onView:weakSelf.view];
//                                [weakSelf bankCardListRequest];
//                            }
//                           
//                            [weakSelf.exDescView removeFromSuperview];
//                            weakSelf.exDescView = nil;
//                        } failure:^(NSInteger code, NSString *msg) {
//
//                        }];
//                    }else  if ([accountType intValue] ==1){
//                        [weakSelf kuaiqianBangDingNetworking:model.code MerchantCode:merchantCode Phone:model.userPhone];
//                    }else  if ([accountType intValue] ==2){
//                        [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],GDMERCHANT_BINDBANKCARD) withParameter:@{@"merchantCode":merchantCode, @"code":code} withVC:weakSelf netName:@"绑卡光大" success:^(id response, NSString *msg) {
//
//                            [XSInfoView showInfo:msg onView:weakSelf.view];
//                            [weakSelf bankCardListRequest];
//
//                            [weakSelf.exDescView removeFromSuperview];
//                            weakSelf.exDescView = nil;
//                        } failure:^(NSInteger code, NSString *msg) {
//
//                        }];
//                    }
//                    [self bankCardListRequest];
//                }
//            };
//            [self.bangDingBarkView.cententButton addTarget:self action:@selector(cententBangDingBark) forControlEvents:(UIControlEventTouchUpInside)];
//            [self.view addSubview:self.bangDingBarkView];
//            
//            completionHandler (YES);
//        }];
//        bindCardAction.backgroundColor = [UIColor colorWithHexString:@"#13ABBE"];
//        
//        // 解绑
//        UIContextualAction *unbindCardAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"解绑" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//            [[UIApplication sharedApplication].keyWindow addSubview:weakSelf.exDescView];
//            [self.tableView setEditing:NO animated:YES];
//            weakSelf.exDescView.code = model.code;
//            weakSelf.exDescView.bindCardStatus = model.bindCardStatus;
//            weakSelf.exDescView.hatBindCardStatus = model.hatBindCardStatus;
//            weakSelf.exDescView.gdBindCardStatus = model.gdBindCardStatus;
//            weakSelf.exDescView.block = ^(NSString *code, NSString *merchantCode, NSString *accountType) {
//                if (![merchantCode isNotBlank]) {
//                    [weakSelf.exDescView removeFromSuperview];
//                    weakSelf.exDescView = nil;
//                }
//
//                
//                if ([merchantCode isNotBlank]) {
//                    if ([accountType intValue] ==0) {
//                        [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@/%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],UNBIND_BANK_CARD,model.code) withParameter:nil withVC:self netName:@"解绑浦发" success:^(id response, NSString *msg) {
//                            
//                            [XSInfoView showInfo:msg onView:weakSelf.view];
//                            [weakSelf.exDescView removeFromSuperview];
//                            weakSelf.exDescView = nil;
//                            [weakSelf bankCardListRequest];
//                            
//                            completionHandler (YES);
//                            
//                        } failure:^(NSInteger code, NSString *msg) {
//                            
//                        }];
//                    }else if ([accountType intValue] ==1) {
//                        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//                        [dic setValue:merchantCode forKey:@"merchantCode"];
//                        [dic setValue:model.code forKey:@"code"];
//                        [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],HATMErCHANT_BANK_CARD) withParameter:dic withVC:self netName:@"解绑快钱" success:^(id response, NSString *msg) {
//                            
//                            [XSInfoView showInfo:msg onView:weakSelf.view];
//                            [weakSelf.exDescView removeFromSuperview];
//                            weakSelf.exDescView = nil;
//                            [weakSelf bankCardListRequest];
//                            
//                            completionHandler (YES);
//                            
//                        } failure:^(NSInteger code, NSString *msg) {
//                            
//                        }];
//                    }else if ([accountType intValue] ==2) {
//                        [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@/%@/%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],GDTMErCHANT_UNBINGBANKCARDFORMULTIPLETAZ,model.code,merchantCode) withParameter:nil withVC:self netName:@"解绑光大" success:^(id response, NSString *msg) {
//                            
//                            [XSInfoView showInfo:msg onView:weakSelf.view];
//                            [weakSelf.exDescView removeFromSuperview];
//                            weakSelf.exDescView = nil;
//                            [weakSelf bankCardListRequest];
//                            
//                            completionHandler (YES);
//                            
//                        } failure:^(NSInteger code, NSString *msg) {
//                            
//                        }];
//                    }
//                    [self bankCardListRequest];
//                }
//            };
//        }];
//        unbindCardAction.backgroundColor = [UIColor colorWithHexString:@"#FF9000"];
//        
//        // 更新
//        UIContextualAction *updateAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"更新" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//            
//            [self.tableView setEditing:NO animated:YES];
//
//            
//            completionHandler (YES);
//        }];
//        updateAction.backgroundColor = [UIColor colorWithHexString:@"#13ABBE"];
//        
//        // 验证
//        UIContextualAction *verfiyAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"验证" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
//            
//            [self.tableView setEditing:NO animated:YES];
//
//            
//            completionHandler (YES);
//        }];
//        verfiyAction.backgroundColor = [UIColor colorWithHexString:@"#32B16C"];
//
//        
//        UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[]];
////        int type = 0;
////        if (model.bindCardStatus == 0 && [model.hatBindCardStatus intValue] == 0) {
////            type = 0;
////        }else if (model.bindCardStatus == 1 || [model.hatBindCardStatus intValue] == 1){
////            type = 1;
////        }else if (model.bindCardStatus == 2 || [model.hatBindCardStatus intValue] == 2){
////            type = 2;
////        }else if (model.bindCardStatus == 3 && [model.hatBindCardStatus intValue] == 3){
////            type = 3;
////        }else if (model.bindCardStatus == 3 || [model.hatBindCardStatus intValue] == 3){
////            type = 5;
////        }else if (model.bindCardStatus == 4 || [model.hatBindCardStatus intValue] == 4){
////            type = 4;
////        }
////        switch (type) {
////            case 0:
////                // 未绑卡
////                actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction, setEditAction, bindCardAction, setDefultAction]];
////                break;
////                
////            case 1:
////                // 处理中
////                actions = [UISwipeActionsConfiguration configurationWithActions:@[setDefultAction]];
////                break;
////                
////            case 2:
////                // 待验证
////                actions = [UISwipeActionsConfiguration configurationWithActions:@[setEditAction, deleteAction, verfiyAction, setDefultAction]];
////                break;
////                
////            case 3:
////                // 已绑卡
////                actions = [UISwipeActionsConfiguration configurationWithActions:@[unbindCardAction, setDefultAction]];
////                break;
////                
////            case 4:
////                // 已失败
////                actions = [UISwipeActionsConfiguration configurationWithActions:@[bindCardAction, deleteAction, setEditAction, setDefultAction]];
////                break;
////            case 5:
////                // 绑卡一张
////                actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction, setEditAction, bindCardAction, setDefultAction,unbindCardAction]];
////                break;
////            default:
////                break;
////        }
//        actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction, setEditAction, bindCardAction,unbindCardAction, setDefultAction]];
//
//        actions.performsFirstActionWithFullSwipe = NO;
//
//        if (model.isDefault == 1) {
//            NSMutableArray<UIContextualAction *> *actionsMArr = [[NSMutableArray alloc] initWithArray:actions.actions];
//            for (int i = 0; i < actionsMArr.count; i++) {
//                UIContextualAction *action = actionsMArr[i];
//                if ([action isEqual:setDefultAction]) {
//                    [actionsMArr removeObject:action];
//                    i--;
//                }
//            }
//
//            actions = [UISwipeActionsConfiguration configurationWithActions:actionsMArr];
//            actions.performsFirstActionWithFullSwipe = NO;
//            return actions;
//        }
//        
//        return actions;
//
//    } else {
//        // Fallback on earlier versions
//    }
//    
//    return nil;
//}
- (void)cententBangDingBark{
    [self.bangDingBarkView removeFromSuperview];
}
/*
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF

    BankCardListModel *model = self.dataArray[indexPath.row];

    // 添加一个删除按钮
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        GeneralPopUp *generalPopUp = [[GeneralPopUp alloc] initWithFrame:CGRectMake(0, 0, ScreenW-100, 240)];
        cut(generalPopUp, 5);
        generalPopUp.backgroundColor = [UIColor whiteColor];
        generalPopUp.mainTitle = @"温馨提示";
        generalPopUp.subTitle = @"你确定要删除该银行卡吗？";
        generalPopUp.handleBtnTitleArray = @[@"取消", @"确定"];
        [[GlobalPopUpTool sharedInstance] popUpWithView:generalPopUp withViewController:self withAnimation:YES withCanTapBgCancel:YES];
        generalPopUp.btnActionResultBlock = ^(BOOL isConfirm, NSString * _Nonnull title, NSInteger index) {

            [[GlobalPopUpTool sharedInstance] removePopUpView];

            if (isConfirm) {

                NSDictionary *params = @{
                    @"codes": model.code,
                    @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
                };
                [HTTPManager deleteNetworkRequest:CJStringWithFormat(@"%@%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],BANKCARD) withParameter:params withVC:self netName:@"删除银行卡" success:^(id response, NSString *msg) {

                    [XSInfoView showInfo:msg onView:weakSelf.view];

                    // 首先修改model
                    [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
                    // 之后更新view
                    [weakSelf.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

                } failure:^(NSInteger code, NSString *msg) {

                }];
            }
        };

    }];

    // 添加一个编辑按钮
    UITableViewRowAction *setEditAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"编辑"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        BandCardAddViewController *bcavc = [[BandCardAddViewController alloc] init];
        bcavc.isEdit = YES;
        bcavc.model = model;
        [self.navigationController pushViewController:bcavc animated:YES];

    }];
    setEditAction.backgroundColor = Golden_Color;

    if (model.isDefault == 1) {

        return @[deleteRowAction, setEditAction];

    } else {
        //操作非默认银行卡
        UITableViewRowAction *setDefultAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"默认"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

            [HTTPManager putNetworkRequest:CJStringWithFormat(@"%@%@/%@",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl],BANKCARD_SETDEFAULTCARD,model.code) withParameter:nil withVC:self netName:@"设置默认银行卡" success:^(id response, NSString *msg) {

                [XSInfoView showInfo:msg onView:weakSelf.view];

                // 首先修改model
                model.isDefault = 1;
                // 之后更新view
                [weakSelf.tableView reloadRow:indexPath.row inSection:0 withRowAnimation:UITableViewRowAnimationAutomatic];

            } failure:^(NSInteger code, NSString *msg) {

            }];

        }];
        setDefultAction.backgroundColor = UIColorHex(0x54a9dd);

        return @[deleteRowAction, setEditAction, setDefultAction];

    }

}
 
*/

//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//
//}
//- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return @"删除";
//}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//
//}
//快钱绑卡受理接口
- (void)kuaiqianBangDingNetworking:(NSString *)backCode MerchantCode:(NSString *)merchantCode Phone:(NSString *)phone{
    WEAKSELF
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:backCode forKey:@"code"];
    [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@/hatMerchant/bindBankCard/accept",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl]) withParameter:dic withVC:weakSelf netName:@"快钱绑卡受理接口" success:^(id response, NSString *msg) {
        weakSelf.backCodeview = [[ZTXBangDingBackCodeView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        [weakSelf.backCodeview upDataPhone:phone];
        weakSelf.backCodeview.blockJianQuanCode = ^{
            [weakSelf kuaiqianBangDingJianQuanNetworking:backCode MerchantCode:merchantCode];
        };
        weakSelf.backCodeview.blockCommintCode = ^(NSString * _Nonnull code) {
            [weakSelf kuaiqianBangDingBangNetworking:backCode jianQuanCode:code MerchantCode:merchantCode];
        };
        weakSelf.backCodeview.blockCententButton = ^{
            [weakSelf.backCodeview removeFromSuperview];
        };
        [weakSelf.view addSubview:weakSelf.backCodeview];
        [weakSelf.exDescView removeFromSuperview];
        weakSelf.exDescView = nil;
        [XSInfoView showInfo:msg onView:weakSelf.view];
    } failure:^(NSInteger code, NSString *msg) {

    }];
}
- (void)kuaiqianBangDingJianQuanNetworking:(NSString *)backCode MerchantCode:(NSString *)merchantCode{//快钱鉴权接口
    WEAKSELF
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:backCode forKey:@"code"];
    [dic setValue:merchantCode forKey:@"merchantCode"];//快钱商户code
    [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@/hatMerchant/bindBankCard/auth",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl]) withParameter:dic withVC:weakSelf netName:@"快钱绑卡鉴权接口" success:^(id response, NSString *msg) {
        
        [XSInfoView showInfo:msg onView:weakSelf.view];
    } failure:^(NSInteger code, NSString *msg) {

    }];
}
//快钱绑卡
- (void)kuaiqianBangDingBangNetworking:(NSString *)backCode jianQuanCode:(NSString *)jianCode MerchantCode:(NSString *)merchantCode{
    WEAKSELF
    if (merchantCode.length <= 0) {
        [XSInfoView showInfo:@"请选择商户" onView:weakSelf.view];
        return;
    }
    if (jianCode.length <= 0) {
        [XSInfoView showInfo:@"请输入验证码" onView:weakSelf.view];
        return;
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:backCode forKey:@"code"];
    [dic setValue:merchantCode forKey:@"merchantCode"];//快钱商户code
    [dic setValue:jianCode forKey:@"validCode"];
    [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@/hatMerchant/bindBankCard",[NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl]) withParameter:dic withVC:weakSelf netName:@"快钱绑卡接口" success:^(id response, NSString *msg) {
        
        [XSInfoView showInfo:msg onView:weakSelf.view];
        [weakSelf bankCardListRequest];
    } failure:^(NSInteger code, NSString *msg) {

    }];
}

#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 30, ScreenW, ScreenH - NavHeight - MenuBarHeight - 75 - 30);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BindCardCell class]) bundle:nil] forCellReuseIdentifier:cellID];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
        _foundImageView.image = image(@"gcy_img_list_found");
        _foundImageView.contentMode = UIViewContentModeScaleAspectFit;
        _foundImageView.center = self.view.center;
        _foundImageView.cy_centerY-=60;
        [self.view addSubview:_foundImageView];
    }
    return _foundImageView;
}

- (UIButton *)addCardButton {
    if (!_addCardButton) {
        _addCardButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addCardButton setTitle:@"新增银行卡" forState:UIControlStateNormal];
        _addCardButton.titleLabel.font = font(16);
        cut(_addCardButton, BTN_BORDER_RADIUS);
        [_addCardButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_addCardButton setBackgroundColor:Theme_ColorValue];
        [_addCardButton addTarget:self action:@selector(btnEventClick) forControlEvents:UIControlEventTouchUpInside];
        _addCardButton.frame = CGRectMake(20, ScreenH - NavHeight - MenuBarHeight - 65, ScreenW-40, 45);
    }
    return _addCardButton;
}

- (NYSExcDescView *)exDescView {
    if (!_exDescView) {
        CGFloat width = ScreenW;
        CGFloat height = ScreenH;
        _exDescView = [[NYSExcDescView alloc] initWithFrame:CGRectMake(0, 0, width, height) upcode:cussentModel.code];
    }
    return _exDescView;
}

@end
